package org.wyf.EveryPoint.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.wyf.EveryPoint.common.constants.D;
import org.wyf.EveryPoint.common.controller.BaseController;
import org.wyf.EveryPoint.common.entity.FebsConstant;
import org.wyf.EveryPoint.common.entity.FebsResponse;
import org.wyf.EveryPoint.common.service.IDictItemService;
import org.wyf.EveryPoint.common.utils.DateUtil;
import org.wyf.EveryPoint.common.utils.FebsUtil;
import org.wyf.EveryPoint.crowd.entity.*;
import org.wyf.EveryPoint.crowd.service.*;
import org.wyf.EveryPoint.system.entity.User;
import org.wyf.EveryPoint.system.service.IUserDataPermissionService;
import org.wyf.EveryPoint.system.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.ExpiredSessionException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author MrBird
 */
@Controller("systemView")
@RequiredArgsConstructor
public class ViewController extends BaseController {

    private final IUserService userService;
    private final IUserDataPermissionService userDataPermissionService;
    private final IProjectService projectService;
    private final IRewardService rewardService;
    private final ITeamService teamService;
    private final IDictItemService dictItemService;
    private final ICollectionProjectService collectionProjectService;
    private final ICommentService commentService;
    private final IOrderService orderService;


    @GetMapping("login")
    @ResponseBody
    public Object login(HttpServletRequest request) {
        if (FebsUtil.isAjaxRequest(request)) {
            throw new ExpiredSessionException();
        } else {
            ModelAndView mav = new ModelAndView();
            mav.setViewName(FebsUtil.view("login"));
            return mav;
        }
    }

    @GetMapping("unauthorized")
    public String unauthorized() {
        return FebsUtil.view("error/403");
    }


    @GetMapping("/")
    public String redirectIndex() {
        return "redirect:/web/common/index";
    }

    @GetMapping("index")
    public String index(Model model) {
        User principal = userService.findByName(getCurrentUser().getUsername());
        userService.doGetUserAuthorizationInfo(principal);
        principal.setPassword("It's a secret");
        model.addAttribute("user", principal);
        model.addAttribute("permissions", principal.getStringPermissions());
        model.addAttribute("roles", principal.getRoles());
//        if(principal.getIsCreate() == null || "0".equals(principal.getIsCreate())){
//            return FebsUtil.view("error/403");
//        }
//        if(principal.getIsAdmin() == null || "0".equals(principal.getIsAdmin())){
//            return FebsUtil.view("error/403");
//        }
        return "index";
    }

    @GetMapping("/web/common/index")
    public String webIndex(Model model) {
        if (getCurrentUser() != null) {
            User principal = userService.findByName(getCurrentUser().getUsername());
            userService.doGetUserAuthorizationInfo(principal);
            principal.setPassword("It's a secret");
            model.addAttribute("user", principal);
            model.addAttribute("permissions", principal.getStringPermissions());
            model.addAttribute("roles", principal.getRoles());
            if (principal.getRealName() == null || "".equals(principal.getRealName())) {
                model.addAttribute("needEdit", true);
            } else {
                model.addAttribute("needEdit", false);
            }
        }
        return FebsUtil.front("index");
    }

    @GetMapping("/web/common/login")
    public Object webLogin(HttpServletRequest request, String type) {
        if (FebsUtil.isAjaxRequest(request)) {
            throw new ExpiredSessionException();
        } else {
            ModelAndView mav = new ModelAndView();
            mav.addObject("type", type);
            mav.setViewName(FebsUtil.front("/crowd/login"));
            return mav;
        }
    }


    //    @GetMapping("/web/common/register")
//    public Object webRegister(HttpServletRequest request) {
//        if (FebsUtil.isAjaxRequest(request)) {
//            throw new ExpiredSessionException();
//        } else {
//            ModelAndView mav = new ModelAndView();
//            mav.setViewName(FebsUtil.front("/crowd/register"));
//            return mav;
//        }
//    }
    @GetMapping(FebsConstant.VIEW_PREFIX + "layout")
    public String layout() {
        return FebsUtil.view("layout");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "password/update")
    public String passwordUpdate() {
        return FebsUtil.view("system/user/passwordUpdate");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "user/profile")
    public String userProfile() {
        return FebsUtil.view("system/user/userProfile");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "user/avatar")
    public String userAvatar() {
        return FebsUtil.view("system/user/avatar");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "user/profile/update")
    public String profileUpdate() {
        return FebsUtil.view("system/user/profileUpdate");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "system/user")
    @RequiresPermissions("user:view")
    public String systemUser() {
        return FebsUtil.view("system/user/user");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "system/user/add")
    @RequiresPermissions("user:add")
    public String systemUserAdd() {
        return FebsUtil.view("system/user/userAdd");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "system/user/detail/{username}")
    @RequiresPermissions("user:view")
    public String systemUserDetail(@PathVariable String username, Model model) {
        resolveUserModel(username, model, true);
        return FebsUtil.view("system/user/userDetail");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "system/user/update/{username}")
    @RequiresPermissions("user:update")
    public String systemUserUpdate(@PathVariable String username, Model model) {
        resolveUserModel(username, model, false);
        return FebsUtil.view("system/user/userUpdate");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "system/role")
    @RequiresPermissions("role:view")
    public String systemRole() {
        return FebsUtil.view("system/role/role");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "system/menu")
    @RequiresPermissions("menu:view")
    public String systemMenu() {
        return FebsUtil.view("system/menu/menu");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "system/dept")
    @RequiresPermissions("dept:view")
    public String systemDept() {
        return FebsUtil.view("system/dept/dept");
    }

    @RequestMapping(FebsConstant.VIEW_PREFIX + "index")
    public String pageIndex() {
        return FebsUtil.view("index");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "404")
    public String error404() {
        return FebsUtil.view("error/404");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "403")
    public String error403() {
        return FebsUtil.view("error/403");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "500")
    public String error500() {
        return FebsUtil.view("error/500");
    }

    private void resolveUserModel(String username, Model model, Boolean transform) {
        User user = userService.findByName(username);
        String deptIds = userDataPermissionService.findByUserId(String.valueOf(user.getUserId()));
        user.setDeptIds(deptIds);
        model.addAttribute("user", user);
        if (transform) {
            String sex = user.getSex();
            switch (sex) {
                case User.SEX_MALE:
                    user.setSex("男");
                    break;
                case User.SEX_FEMALE:
                    user.setSex("女");
                    break;
                default:
                    user.setSex("保密");
                    break;
            }
        }
        if (user.getLastLoginTime() != null) {
            model.addAttribute("lastLoginTime", DateUtil.getDateFormat(user.getLastLoginTime(), DateUtil.FULL_TIME_SPLIT_PATTERN));
        }
    }


    @GetMapping("/web/createProject")
    public String createProject(Model model) {
        User principal = userService.findByName(getCurrentUser().getUsername());
        userService.doGetUserAuthorizationInfo(principal);
        principal.setPassword("It's a secret");
        model.addAttribute("user", principal);
        model.addAttribute("permissions", principal.getStringPermissions());
        model.addAttribute("roles", principal.getRoles());
        if (principal.getRealName() == null || "".equals(principal.getRealName())) {
            model.addAttribute("needEdit", true);
        } else {
            model.addAttribute("needEdit", false);
        }
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", principal.getUserId()).eq("state", 0);
        Project project = projectService.getOne(queryWrapper);
        if (project != null) {
            model.addAttribute("project", project);
            return FebsUtil.front("crowd/project_base");
        }
        return FebsUtil.front("/crowd/createProject");
    }

    @GetMapping("/web/project_base")
    public String project_base(Long id, Model model) {
        Project project = projectService.getById(id);
        if (project == null) {
            return "redirect:" + FebsConstant.VIEW_PREFIX + "404";
        }
        if (project.getState() != 0) {
            return "redirect:" + FebsConstant.VIEW_PREFIX + "403";
        }
        if (!project.getUid().equals(getCurrentUser().getUserId())) {
            return "redirect:" + FebsConstant.VIEW_PREFIX + "403";
        }
        model.addAttribute("project", project);
        if (getCurrentUser() != null) {
            User principal = userService.findByName(getCurrentUser().getUsername());
            userService.doGetUserAuthorizationInfo(principal);
            principal.setPassword("It's a secret");
            model.addAttribute("user", principal);
            model.addAttribute("permissions", principal.getStringPermissions());
            model.addAttribute("roles", principal.getRoles());
        }
        return FebsUtil.front("crowd/project_base");
    }


    @GetMapping("/web/project_material")
    public String project_material(Long id, Model model) {
        Project project = projectService.getById(id);
        if (project == null) {
            return "redirect:" + FebsConstant.VIEW_PREFIX + "404";
        }
        if (project.getState() != 0) {
            return "redirect:" + FebsConstant.VIEW_PREFIX + "403";
        }
        if (!project.getUid().equals(getCurrentUser().getUserId())) {
            return "redirect:" + FebsConstant.VIEW_PREFIX + "403";
        }
        model.addAttribute("project", project);
        User principal = userService.findByName(getCurrentUser().getUsername());
        userService.doGetUserAuthorizationInfo(principal);
        principal.setPassword("It's a secret");
        model.addAttribute("user", principal);
        model.addAttribute("permissions", principal.getStringPermissions());
        model.addAttribute("roles", principal.getRoles());
        return FebsUtil.front("crowd/project_material");
    }


    @GetMapping("/web/project_detail")
    public String project_detail(Long id, Model model) {
        Project project = projectService.getById(id);
        if (project == null) {
            return "redirect:" + FebsConstant.VIEW_PREFIX + "404";
        }
        if (project.getState() != 0) {
            return "redirect:" + FebsConstant.VIEW_PREFIX + "403";
        }
        if (!project.getUid().equals(getCurrentUser().getUserId())) {
            return "redirect:" + FebsConstant.VIEW_PREFIX + "403";
        }
        model.addAttribute("project", project);
        User principal = userService.findByName(getCurrentUser().getUsername());
        userService.doGetUserAuthorizationInfo(principal);
        principal.setPassword("It's a secret");
        model.addAttribute("user", principal);
        model.addAttribute("permissions", principal.getStringPermissions());
        model.addAttribute("roles", principal.getRoles());
        return FebsUtil.front("crowd/project_detail");
    }

    @GetMapping("/web/project_reward")
    public String project_reward(Long id, Model model) {
        Project project = projectService.getById(id);
        if (project == null) {
            return "redirect:" + FebsConstant.VIEW_PREFIX + "404";
        }
        if (project.getState() != 0) {
            return "redirect:" + FebsConstant.VIEW_PREFIX + "403";
        }
        if (!project.getUid().equals(getCurrentUser().getUserId())) {
            return "redirect:" + FebsConstant.VIEW_PREFIX + "403";
        }
        QueryWrapper<Reward> queryWrapper = new QueryWrapper();
        queryWrapper.eq("pid", id).eq("type", "1");
        List<Reward> rewards = rewardService.list(queryWrapper);
        model.addAttribute("rewards", rewards);
        model.addAttribute("project", project);
        User principal = userService.findByName(getCurrentUser().getUsername());
        userService.doGetUserAuthorizationInfo(principal);
        principal.setPassword("It's a secret");
        model.addAttribute("user", principal);
        model.addAttribute("permissions", principal.getStringPermissions());
        model.addAttribute("roles", principal.getRoles());
        return FebsUtil.front("crowd/project_reward");
    }

    @GetMapping("/web/project_team")
    public String project_team(Long id, Model model) {
        Project project = projectService.getById(id);
        if (project.getState() != 0) {
            return "redirect:" + FebsConstant.VIEW_PREFIX + "403";
        }
        Team team = new Team();
        team.setPid(id);
        team.setUid(getCurrentUser().getUserId());
        team.setLeader(1);
        List<Team> list = teamService.findProjectTeams(getCurrentUser().getUserId(),id);
        if (list.size()<=0){
            teamService.createTeam(team);
        }
        if (project == null) {
            return "redirect:" + FebsConstant.VIEW_PREFIX + "404";
        }
        if (!project.getUid().equals(getCurrentUser().getUserId())) {
            return "redirect:" + FebsConstant.VIEW_PREFIX + "403";
        }
        model.addAttribute("project", project);
        User principal = userService.findByName(getCurrentUser().getUsername());
        userService.doGetUserAuthorizationInfo(principal);
        principal.setPassword("It's a secret");
        model.addAttribute("user", principal);
        model.addAttribute("permissions", principal.getStringPermissions());
        model.addAttribute("roles", principal.getRoles());
        return FebsUtil.front("crowd/project_team");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "system/user/project/apply")
    @RequiresPermissions("project:view")
    public String userProjectApply(Model model) {
        model.addAttribute("type", 3);
        return FebsUtil.view("system/user/projectManage");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "system/project")
    @RequiresPermissions("project:view")
    public String systemProject(Model model) {
        model.addAttribute("type", 3);
        return FebsUtil.view("system/project/project");
    }

    @GetMapping("/web/common/aboutUs")
    public String aboutUs(Model model) {
        if (getCurrentUser() != null) {
            User principal = userService.findByName(getCurrentUser().getUsername());
            userService.doGetUserAuthorizationInfo(principal);
            principal.setPassword("It's a secret");
            model.addAttribute("user", principal);
            model.addAttribute("permissions", principal.getStringPermissions());
            model.addAttribute("roles", principal.getRoles());
        }
        return FebsUtil.front("crowd/aboutUs");
    }

    @GetMapping("/web/project_info")
    public String project_info(Long projectId, Model model) {
        // 获取当前用户信息
        User principal = userService.findByName(getCurrentUser().getUsername());
        userService.doGetUserAuthorizationInfo(principal);
        principal.setPassword("It's a secret");
        model.addAttribute("user", principal);
        model.addAttribute("permissions", principal.getStringPermissions());
        model.addAttribute("roles", principal.getRoles());
        if (principal.getRealName() == null || "".equals(principal.getRealName())) {
            model.addAttribute("needEdit", true);
        } else {
            model.addAttribute("needEdit", false);
        }

        // 获取项目信息
        Project project = projectService.getById(projectId);
        // 未发布项目不允许查看
//        if (project.getState() <= 4) {
//            return "redirect:" + FebsConstant.VIEW_PREFIX + "403";
//        }
        project.setHitTimes(project.getHitTimes() + 1);
        projectService.updateById(project);
        model.addAttribute("project", project);
        //获取回报设置
        QueryWrapper<Reward> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", projectId).eq("type", "1");
        List<Reward> reward = rewardService.list(queryWrapper);
        model.addAttribute("rewards", reward);
        // 获取贡献排行
        model.addAttribute("profile", "贡献者");
        // 获取分类
        String type = dictItemService.findDictItemName(D.PROJECT_TYPE, project.getType());
        model.addAttribute("type", type);
        // 获取用户是否收藏
        List<CollectionProject> list = collectionProjectService.listUserCollection(projectId,principal.getUserId());
        if (list.size() > 0) {
            model.addAttribute("collectionFlag", true);
        } else {
            model.addAttribute("collectionFlag", false);
        }
        // 获取发布者信息
        User creater = userService.getById(project.getUid());
        creater.setPassword("It's a secret");
        model.addAttribute("creater", creater);
        return FebsUtil.front("crowd/project_info");

    }

    @GetMapping("/web/project_list_grid")
    public String project_list_grid(String keyWord, Model model) {
        if (getCurrentUser() != null) {
            User principal = userService.findByName(getCurrentUser().getUsername());
            userService.doGetUserAuthorizationInfo(principal);
            principal.setPassword("It's a secret");
            model.addAttribute("user", principal);
            model.addAttribute("permissions", principal.getStringPermissions());
            model.addAttribute("roles", principal.getRoles());
        }
        model.addAttribute("keyWord", keyWord);
        return FebsUtil.front("crowd/project_list_grid");
    }

    @GetMapping("/web/project_rewards_choice")
    public String project_rewards_choice(Long projectId, Model model) {
        if (getCurrentUser() != null) {
            User principal = userService.findByName(getCurrentUser().getUsername());
            userService.doGetUserAuthorizationInfo(principal);
            principal.setPassword("It's a secret");
            model.addAttribute("user", principal);
            model.addAttribute("permissions", principal.getStringPermissions());
            model.addAttribute("roles", principal.getRoles());
        }
        // 获取项目信息
        Project project = projectService.getById(projectId);
        if (cn.hutool.core.date.DateUtil.compare(new Date(),project.getFundBegin())<0){
            return FebsUtil.front("orderState/noBegin");
        }
        if (cn.hutool.core.date.DateUtil.compare(new Date(),project.getFundEnd())>0){
            return FebsUtil.front("orderState/end");
        }
        model.addAttribute("project", project);
        model.addAttribute("projectId", projectId);
        return FebsUtil.front("crowd/project_rewards_choice");
    }

    @GetMapping("/web/project_order_operation")
    public String project_order_operation(Long rewardId, Long projectId, Model model) {
        if (getCurrentUser() != null) {
            User principal = userService.findByName(getCurrentUser().getUsername());
            userService.doGetUserAuthorizationInfo(principal);
            principal.setPassword("It's a secret");
            model.addAttribute("user", principal);
            model.addAttribute("permissions", principal.getStringPermissions());
            model.addAttribute("roles", principal.getRoles());
        }
        ;
        model.addAttribute("projectId", projectId);
        Project project = projectService.getById(projectId);
        if (cn.hutool.core.date.DateUtil.compare(new Date(),project.getFundBegin())<0){
            return FebsUtil.front("orderState/noBegin");
        }
        if (cn.hutool.core.date.DateUtil.compare(new Date(),project.getFundEnd())>0){
            return FebsUtil.front("orderState/end");
        }
        if (rewardId == -1) {
            QueryWrapper<Reward> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("pid", projectId).eq("type", "0");
            Reward reward = rewardService.getOne(queryWrapper);
            model.addAttribute("reward", reward);
            return FebsUtil.front("crowd/project_order_operation_1");
        } else {
            Reward reward = rewardService.getById(rewardId);
            model.addAttribute("reward", reward);
            return FebsUtil.front("crowd/project_order_operation_2");
        }
    }

    @GetMapping("/web/project_order_pay")
    public String project_order_pay(Long orderId, Model model) {
        if (getCurrentUser() != null) {
            User principal = userService.findByName(getCurrentUser().getUsername());
            userService.doGetUserAuthorizationInfo(principal);
            principal.setPassword("It's a secret");
            model.addAttribute("user", principal);
            model.addAttribute("permissions", principal.getStringPermissions());
            model.addAttribute("roles", principal.getRoles());
        }
        Order order = orderService.getById(orderId);
        model.addAttribute("order", order);
        if (order.getState() == 2) {
            return FebsUtil.front("orderState/longTime");
        }
        if (order.getState() == 3) {
            return FebsUtil.front("orderState/success");
        }
        Reward reward = rewardService.getById(order.getRid());
        model.addAttribute("reward", reward);
        // 获取项目信息
        Project project = projectService.getById(reward.getPid());
        model.addAttribute("project", project);
        if (order.getState() == 1) {
            return FebsUtil.front("crowd/project_order_pay");
        } else {
            return FebsUtil.front("orderState/close");
        }
    }

    @GetMapping("/web/project_order_pay_after")
    public String project_order_pay_after(String orderId, Model model) {
        if (getCurrentUser() != null) {
            User principal = userService.findByName(getCurrentUser().getUsername());
            userService.doGetUserAuthorizationInfo(principal);
            principal.setPassword("It's a secret");
            model.addAttribute("user", principal);
            model.addAttribute("permissions", principal.getStringPermissions());
            model.addAttribute("roles", principal.getRoles());
        }
        Order order = orderService.getById(orderId);
        model.addAttribute("order", order);
        Reward reward = rewardService.getById(order.getRid());
        model.addAttribute("reward", reward);
        // 获取项目信息
        Project project = projectService.getById(reward.getPid());
        model.addAttribute("project", project);
        return FebsUtil.front("crowd/project_order_pay_after");
    }

    @GetMapping("/web/user/info/edit")
    public String userInfoEdit(Model model) {
        if (getCurrentUser() != null) {
            User principal = userService.findByName(getCurrentUser().getUsername());
            userService.doGetUserAuthorizationInfo(principal);
            principal.setPassword("secret");
            model.addAttribute("user", principal);
            model.addAttribute("permissions", principal.getStringPermissions());
            model.addAttribute("roles", principal.getRoles());
            model.addAttribute("sex", principal.getSex());
            model.addAttribute("deptId", principal.getDeptId());
        }
        return FebsUtil.front("user/userInfo");
    }

    @GetMapping("/web/user/avatar/update")
    public String webUserAvatar() {
        return FebsUtil.front("user/avatar");
    }

    @GetMapping("/web/user/order/list")
    public String userOrderList(Model model) {
        User principal = userService.findByName(getCurrentUser().getUsername());
        userService.doGetUserAuthorizationInfo(principal);
        principal.setPassword("secret");
        model.addAttribute("user", principal);
        return FebsUtil.front("user/userOrder");
    }

    @GetMapping("/web/view/order/getOrderInfo")
    public String getOrderInfo(String orderId,Model model) {
        User principal = userService.findByName(getCurrentUser().getUsername());
        userService.doGetUserAuthorizationInfo(principal);
        principal.setPassword("secret");
        model.addAttribute("user", principal);
        Order order = orderService.getById(orderId);
        String dateFormat = DateUtil.getDateFormat(order.getCreateTime(), DateUtil.FULL_TIME_SPLIT_PATTERN);
        model.addAttribute("order", order);
        model.addAttribute("dateFormat", dateFormat);
        Project project = projectService.getById(order.getPid());
        model.addAttribute("project", project);
        Reward reward = rewardService.getById(order.getRid());
        model.addAttribute("reward", reward);
        return FebsUtil.front("orderState/orderInfo");
    }

    @GetMapping("/web/user/collection/list")
    public String userCollectionList(Model model) {
        User principal = userService.findByName(getCurrentUser().getUsername());
        userService.doGetUserAuthorizationInfo(principal);
        principal.setPassword("secret");
        model.addAttribute("user", principal);
        return FebsUtil.front("user/userCollection");
    }

    @GetMapping("/web/user/project/list")
    public String userProjectList(Model model) {
        User principal = userService.findByName(getCurrentUser().getUsername());
        userService.doGetUserAuthorizationInfo(principal);
        principal.setPassword("secret");
        model.addAttribute("user", principal);
        return FebsUtil.front("user/userProject");
    }

    @GetMapping("/web/project_preView")
    public String project_preView(Long projectId, Model model) {
        // 获取当前用户信息
        User principal = userService.findByName(getCurrentUser().getUsername());
        userService.doGetUserAuthorizationInfo(principal);
        principal.setPassword("It's a secret");
        model.addAttribute("user", principal);
        model.addAttribute("permissions", principal.getStringPermissions());
        model.addAttribute("roles", principal.getRoles());
        if (principal.getRealName() == null || "".equals(principal.getRealName())) {
            model.addAttribute("needEdit", true);
        } else {
            model.addAttribute("needEdit", false);
        }
        // 获取项目信息
        Project project = projectService.getById(projectId);
        if (!project.getUid().equals(principal.getUserId())){
            return "redirect:" + FebsConstant.VIEW_PREFIX + "403";
        }
        model.addAttribute("project", project);
        //获取回报设置
        QueryWrapper<Reward> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", projectId).eq("type", "1");
        List<Reward> reward = rewardService.list(queryWrapper);
        model.addAttribute("rewards", reward);
        // 获取贡献排行
        model.addAttribute("profile", "贡献者");
        // 获取分类
        String type = dictItemService.findDictItemName(D.PROJECT_TYPE, project.getType());
        model.addAttribute("type", type);
        // 获取用户是否收藏
        List<CollectionProject> list = collectionProjectService.listUserCollection(projectId,principal.getUserId());
        if (list.size() > 0) {
            model.addAttribute("collectionFlag", true);
        } else {
            model.addAttribute("collectionFlag", false);
        }
        // 获取发布者信息
        User creater = userService.getById(project.getUid());
        creater.setPassword("It's a secret");
        model.addAttribute("creater", creater);
        return FebsUtil.front("crowd/project_preView");

    }

    @GetMapping("/web/admin/project_preView")
    public String project_adminPreview(Long projectId, Model model) {
        // 获取项目信息
        Project project = projectService.getById(projectId);
        projectService.updateById(project);
        model.addAttribute("project", project);
        //获取回报设置
        QueryWrapper<Reward> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", projectId).eq("type", "1");
        List<Reward> reward = rewardService.list(queryWrapper);
        model.addAttribute("rewards", reward);
        // 获取分类
        String type = dictItemService.findDictItemName(D.PROJECT_TYPE, project.getType());
        model.addAttribute("type", type);
        // 获取发布者信息
        User creater = userService.getById(project.getUid());
        creater.setPassword("It's a secret");
        model.addAttribute("creater", creater);
        return FebsUtil.view("system/project/project_detail");

    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "system/project/exam/{projectId}")
    public String projectExam(@PathVariable Long projectId, Model model) {
        model.addAttribute("projectId",projectId);
        return FebsUtil.view("system/project/project_exam");

    }
}
